{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 09\n",
    "# 定义、使用父类 (动物)、子类 (鸡、兔、猪)\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 父类，动物\n",
    "class Animal:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def eat(self):\n",
    "        print(f\"{self.name} is eating.\")\n",
    "\n",
    "    def sleep(self):\n",
    "        print(f\"{self.name} is sleeping.\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:15:37.474386Z",
     "end_time": "2024-07-08T21:15:37.489664Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 子类，鸡\n",
    "class Chicken(Animal):\n",
    "    def __init__(self, name, age, color):\n",
    "        super().__init__(name, age)\n",
    "        self.color = color\n",
    "\n",
    "    def lay_egg(self):\n",
    "        print(f\"{self.name} is laying an egg.\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:15:46.940040Z",
     "end_time": "2024-07-08T21:15:46.965041Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 子类，兔\n",
    "class Rabbit(Animal):\n",
    "    def __init__(self, name, age, speed):\n",
    "        super().__init__(name, age)\n",
    "        self.speed = speed\n",
    "\n",
    "    def jump(self):\n",
    "        print(f\"{self.name} is jumping.\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:15:57.935609Z",
     "end_time": "2024-07-08T21:15:57.950747Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 子类，猪\n",
    "class Pig(Animal):\n",
    "    def __init__(self, name, age, weight):\n",
    "        super().__init__(name, age)\n",
    "        self.weight = weight\n",
    "\n",
    "    def roll(self):\n",
    "        print(f\"{self.name} is rolling around.\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:16:07.963212Z",
     "end_time": "2024-07-08T21:16:07.980321Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chicken1 is eating.\n",
      "chicken1 is laying an egg.\n"
     ]
    }
   ],
   "source": [
    "chicken1 = Chicken(\"chicken1\", 1, \"white\")\n",
    "chicken1.eat()\n",
    "chicken1.lay_egg()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:16:26.099117Z",
     "end_time": "2024-07-08T21:16:26.108694Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rabbit1 is sleeping.\n",
      "rabbit1 is jumping.\n"
     ]
    }
   ],
   "source": [
    "rabbit1 = Rabbit(\"rabbit1\", 2, 10)\n",
    "rabbit1.sleep()\n",
    "rabbit1.jump()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:16:47.277500Z",
     "end_time": "2024-07-08T21:16:47.291889Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pig1 is eating.\n",
      "pig1 is rolling around.\n"
     ]
    }
   ],
   "source": [
    "pig1 = Pig(\"pig1\", 3, 100)\n",
    "pig1.eat()\n",
    "pig1.roll()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-08T21:17:01.817160Z",
     "end_time": "2024-07-08T21:17:01.827061Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
